package cn.redis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;

public class Main {
	
	private Jedis jedis; 
	
	@Before
	public void init(){
		//	连接redis服务器，127.0.0.1:6379
		jedis = new Jedis("127.0.0.1", 6379);
		//	权限认证
		//jedis.auth("admin");  
	}
	
	@Test
	public void testString(){
		jedis.set("name","xinxin");
		System.out.println(jedis.get("name"));
		
		jedis.append("name", " is my lover");
		System.out.println(jedis.get("name"));
		
		jedis.del("name");
		System.out.println(jedis.get("name"));
		
		jedis.mset("name", "zhangsan", "age", "20");
		jedis.incr("age");
		System.out.println(jedis.get("name") + "-" + jedis.get("age"));
	}
	
	@Test
	public void testMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "xinxin");
		map.put("age", "20");
		map.put("desc", "暂无介绍");
		jedis.hmset("user", map);
		
		List<String> list = jedis.hmget("user", "name", "age");
		System.out.println(list);
		System.out.println(jedis.hvals("user"));
	}
	
	@Test
	public void testList(){
		jedis.del("java framework");  
		System.out.println(jedis.lrange("java framework",0,-1));  
		
		//	栈
		jedis.lpush("java framework", "spring");
		jedis.lpush("java framework","struts");  
		jedis.lpush("java framework","hibernate");  
		//System.out.println(jedis.lpop("java framework"));
		
		//	取出所有数据jedis.lrange是按范围取出,第一个是key，第二个是起始位置，第三个是结束位置，jedis.llen获取长度 -1表示取得所有 
		System.out.println(jedis.lrange("java framework",0,-1));  
		jedis.del("java framework");
		//	队列
		jedis.rpush("java framework","spring");  
		jedis.rpush("java framework","struts");  
		jedis.rpush("java framework","hibernate");  
		//jedis.ltrim("java framework", 0, 1);
		//System.out.println(jedis.lpop("java framework"));
		
		System.out.println(jedis.lrange("java framework",0,-1));
		System.out.println(jedis.llen("java framework"));
	}
	
	@Test
	public void testSet(){
		jedis.sadd("set","liuling");  
		jedis.sadd("set","xinxin");
		jedis.sadd("set","ling");  
		jedis.sadd("set","who");  
		
		jedis.srem("set","who");  
		System.out.println(jedis.smembers("set"));
	}
	
	@Test
	public void testClient(){
		
		new RedisClient().show(); 
	}
	

}
